package svnloc;

import java.util.*;

public class diffAnalyzer 
{
  HashSet<String> exSet;
  HashSet<String> typeSet;
  ArrayList<String> exStr;
  public int del, mod, add, total;

  public diffAnalyzer(HashSet<String> exSet, ArrayList<String> exStr,HashSet<String> fileExt) 
  {
    this.exSet = exSet;
    this.exStr = exStr;
    this.typeSet = fileExt;

    this.del = this.mod = this.add = this.total = 0;
  }


  public String work(int version, ArrayList<String> info) 
  {
    String line, FileName;
    
    System.out.println("Getting    version " + version);
    
    int delAll = 0, modAll = 0, addAll = 0, totalAll = 0;
    int f_add, f_del, f_mod;

    for (int i = 0; i < info.size(); ) 
    {
      line = info.get(i);

      if ( !line.startsWith("Index: "))
      {
        i++;
        continue;
      };

      FileName = line.substring(7);
      
      if ( ! isTargetSrc(FileName.toLowerCase() ) )
      {
        System.out.println ("Skipping   " + FileName + " #" + version);
        i++;
        continue;
      }

      int add = 0, del = 0;
      
      f_add = f_del = f_mod = 0;

      for ( i++; i < info.size(); i++ )
      {
        line = info.get(i);

        if ( line.startsWith("Index: ") )
        {
          break;
        }

        if (line.startsWith("-")) 
        {
          del++;
        } 
        else if (line.startsWith("+"))
        {
          add++;
        }
        else if (line.startsWith("@"))
        {
          if (add - del > 0) 
          {
            f_add += ( add - del);
            f_mod += del;
          } 
          else 
          {
            f_del += ( del - add);
            f_mod += add;
          }
          del = add = 0;
        }
      }
      
      if (add - del > 0) 
      {
        f_add += ( add - del);
        f_mod += del;
      } 
      else 
      {
        f_del += ( del - add);
        f_mod += add;
      }

      System.out.println ("Processing " + FileName + " #" + version + " (+" + f_add + ",-" + f_del + ",." + f_mod +")");
      
      addAll += f_add;
      modAll += f_mod;
      delAll += f_del;
    }
    
    totalAll = addAll + delAll + modAll;
    this.add += addAll;
    this.del += delAll;
    this.mod += modAll;
    this.total += totalAll;
    
    if (totalAll == 0) 
    {
      return null;
    }

    return String.format("%10d,%10d,%10d,%10d,%10d\r\n",version, addAll,delAll, modAll, totalAll);
  }

  private boolean isExcludeFile(String src) 
  {
    if (exSet.contains(src)) 
    {
      return true;
    }

    for (int i = 0; i < exStr.size(); i++) 
    {
      if (src.startsWith(exStr.get(i))) {
        return true;
      }
    }

    return false;
  }

  private boolean isTargetSrc(String src) 
  {
    int index = src.lastIndexOf('.');
    
    if (index == -1)
    {
      if (typeSet.contains(""))
        index = src.length();
      else
        return false;
    }
    
    return typeSet.contains(src.substring(index)) && !isExcludeFile(src);
  }
}
